package cn.zyq.ykb.service.config.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.zyq.ykb.beans.config.ConfigPage;
import cn.zyq.ykb.system.common.BaseService;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.config.ConfigNavigationMapper;
import cn.zyq.ykb.beans.config.ConfigNavigation;
import cn.zyq.ykb.service.config.ConfigNavigationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.UUID;
import java.util.List;

/**
 * 配置-公共Service业务层处理
 *
 * @author SystemAuth
 * @date 2024-01-10
 */
@Service
public class ConfigNavigationServiceImpl  extends BaseService implements ConfigNavigationService {

    @Autowired
    private ConfigNavigationMapper configNavigationMapper;

    /**
     * 查询配置-公共
     *
     * @param configNavigationID 配置-公共主键
     * @return 配置-公共
     */
    @Override
    public ConfigNavigation selectConfigNavigationByConfigNavigationID(Integer configNavigationID) {
        ConfigNavigation configNavigation = configNavigationMapper.selectByPrimaryKey(configNavigationID);
        if (configNavigation.getDeleted() == 1) {
            return null;
        }
        return configNavigation;
    }

    /**
     * 查询配置-公共
     *
     * @param serviceID 业务ID
     * @return 配置-公共
     */
    @Override
    public ConfigNavigation selectConfigNavigationByServiceID(String serviceID) {
        ConfigNavigation param = new ConfigNavigation();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.configNavigationMapper.selectOne(param);
    }

    /**
     * 查询配置-公共列表
     *
     * @param pageParam
     * @return 配置-公共
     */
    @Override
    public PageInfo<ConfigNavigation> selectConfigNavigationList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<ConfigNavigation> list = this.configNavigationMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询配置-公共列表
     *
     * @param pageParam
     * @return 配置-公共
     */
    @Override
    public List<ConfigNavigation> selectConfigNavigationAllList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        return this.configNavigationMapper.list(pageParam.getKv());
    }
    /**
     * 新增配置-公共
     *
     * @param param 配置-公共
     * @return 结果
     */
    @Override
    public int insertConfigNavigation(ConfigNavigation param) {
        ConfigNavigation page = new ConfigNavigation();
        page.setDeleted(0);
        ConfigNavigation configPage = this.configNavigationMapper.selectOne(page);
        if (BeanUtil.isEmpty(configPage)){
            this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
            return this.configNavigationMapper.insertSelective(param);
        }else {
            param.setConfigNavigationID(configPage.getConfigNavigationID());
            this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
            return this.configNavigationMapper.updateByPrimaryKeySelective(param);
        }
    }

    /**
     * 修改配置-公共
     *
     * @param param 配置-公共
     * @return 结果
     */
    @Override
    public int updateConfigNavigation(ConfigNavigation param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.configNavigationMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除配置-公共
     *
     * @param configNavigationIDs 需要删除的配置-公共主键
     * @return 结果
     */
    @Override
    public int deleteConfigNavigationByConfigNavigationIDs(Integer[] configNavigationIDs) {
        int i = 0;
        for (Integer configNavigationID : configNavigationIDs) {
            i += this.deleteConfigNavigationByConfigNavigationID(configNavigationID);
        }
        return i;
    }

    /**
     * 删除配置-公共信息
     *
     * @param configNavigationID 配置-公共主键
     * @return 结果
     */
    @Override
    public int deleteConfigNavigationByConfigNavigationID(Integer configNavigationID) {
        ConfigNavigation configNavigation = this.selectConfigNavigationByConfigNavigationID(configNavigationID);
        if (configNavigation == null) {
            return 0;
        } else {
            ConfigNavigation storeInfo = new ConfigNavigation();
            storeInfo.setConfigNavigationID(configNavigationID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.configNavigationMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }
}
